package com.bishe.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.config.DataException;
import com.bishe.entity.Club;
import com.bishe.entity.User;
import com.bishe.mapper.UserMapper;
import com.bishe.service.ClubService;
import com.bishe.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2024-03-07 14:46:10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Resource
    private ClubService clubService;

    @Override
    public List<Club> selectClub(String id) {
        User user = getById(id);
        if (user == null) {
            throw new DataException("未查到此用户");
        }

        String clubId = user.getClubId();
        if (StrUtil.isNotEmpty(clubId)) {
            return clubService.listByIds(Arrays.asList(clubId.split(","))
            );
        }

        return Collections.emptyList();
    }

    @Override
    public List<Club> otherClub(String id) {
        User user = getById(id);
        if (user == null) {
            throw new DataException("未查到此用户");
        }

        String clubId = user.getClubId();
        if (StrUtil.isNotEmpty(clubId)) {
            List<Club> list = clubService.list();
            List<String> ids = list.stream().map(Club::getId).collect(Collectors.toList());
            List<String> clubIds = Arrays.asList(clubId.split(","));
            List<String> filterId = ids.stream().filter(s -> {
                for (String string : clubIds) {
                    if (s.equals(string)) {
                        return false;
                    }
                }
                return true;
            }).collect(Collectors.toList());
            return clubService.listByIds(filterId);
        }
        return clubService.list();
    }

    @Override
    public List<User> selectClubUser(String id) {
        List<User> list = list();
        if (CollUtil.isEmpty(list)){
            return Collections.emptyList();
        }
        return list.stream().filter(s->StrUtil.isNotEmpty(s.getClubId()) && Arrays.asList(s.getClubId().split(",")).contains(id)).collect(Collectors.toList());
    }
}




